package com.relytosoft.mizar.media.analyze.services.controller.behavior;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.analyze.api.entity.behavior.AyBehaviorTaskSon;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyBehaviorTaskPlanDto;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyBehaviorTaskSonDto;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyTaskIdDto;
import com.relytosoft.mizar.media.analyze.api.pojo.query.AyBehaviorTaskSonQuery;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyBatchVo;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyBehaviorTaskPlanVo;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyBehaviorTaskSonVo;
import com.relytosoft.mizar.media.analyze.services.service.behavior.AyBehaviorTaskService;
import com.relytosoft.mizar.media.analyze.services.service.behavior.AyBehaviorTaskSonService;
import com.relytosoft.mizar.media.common.enums.TaskStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.core.Validate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/6/7 10:28
 */
@Api(tags = "行为分析任务")
@RestController
@RequestMapping("/analyze/behavior/son")
public class AyBehaviorTaskSonController {

    @Resource
    private AyBehaviorTaskSonService ayBehaviorTaskSonService;


    @Resource
    private AyBehaviorTaskService ayBehaviorTaskService;


    @SaCheckPermission("analyze:behavior:son:list")
    @OpLog(msg = "查询子任务列表", opType = OpTypeEnum.SELECT)
    @ApiOperation(value = "查询子任务列表")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> selectAyBehaviorTaskSonPageList(PageQuery pageQuery, AyBehaviorTaskSonQuery ayBehaviorTaskSonQuery) {
        IPage<AyBehaviorTaskSonVo> page = ayBehaviorTaskSonService.selectAyBehaviorTaskSonPageList(pageQuery,
                ayBehaviorTaskSonQuery);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }


    @SaCheckPermission("analyze:behavior:son:add")
    @OpLog(msg = "创建行为分析子任务", opType = OpTypeEnum.INSERT)
    @ApiOperation("创建行为分析子任务")
    @PostMapping({"/insert"})
    public ResponseResult<AyBatchVo> insert(@Validate @RequestBody AyBehaviorTaskSonDto ayBehaviorTaskSonDto) {
        //构建行为分析子任务
        List<AyBehaviorTaskSon> ayBehaviorTaskSonArrayList = new ArrayList<>();
        AyBatchVo ayBatchVo = ayBehaviorTaskSonService.insertAyBehaviorTaskSon(ayBehaviorTaskSonDto, ayBehaviorTaskSonArrayList);
        //判断ayBehaviorTaskSonArrayList是否为空
        if (!ayBehaviorTaskSonArrayList.isEmpty()) {
            //遍历ayBehaviorTaskSonArrayList
            for (AyBehaviorTaskSon ayBehaviorTaskSon : ayBehaviorTaskSonArrayList) {
                //发送到消息队列
                ayBehaviorTaskSonService.sendMessage(ayBehaviorTaskSon);
            }
        }
        return ResponseResult.ok(ayBatchVo);
    }


    @SaCheckPermission("analyze:behavior:son:delete")
    @OpLog(msg = "删除行为分析子任务", opType = OpTypeEnum.DELETE)
    @ApiOperation("删除行为分析子任务")
    @PutMapping("/delete")
    public ResponseResult<Void> delete(@RequestBody List<Integer> ids) {
        //验证ids是否为空
        if (ids.isEmpty()) {
            return ResponseResult.fail("未选择要删除的行为分析任务");
        }
        //删除行为分析子任务
        boolean isDelete = ayBehaviorTaskSonService.deleteAyBehaviorTaskSon(ids);
        //判断isDelete是否为true
        if (isDelete) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.fail("删除行为分析子任务失败");
        }
    }


    @SaCheckPermission("analyze:behavior:son:start")
    @OpLog(msg = "启动行为分析子任务", opType = OpTypeEnum.OTHER)
    @ApiOperation("启动行为分析子任务")
    @PostMapping({"/start"})
    public ResponseResult<AyBatchVo> start(@RequestBody AyTaskIdDto ayTaskIdDto) {
        //验证参数并获取可以启动的子任务
        AyBatchVo ayBatchVo = new AyBatchVo();
        List<AyBehaviorTaskSon> ayBehaviorTaskSonArrayList = ayBehaviorTaskSonService.canStartAyBehaviorTaskSon(ayTaskIdDto, ayBatchVo);
        //需要发送消息的集合
        List<AyBehaviorTaskSon> insertTaskSons = new ArrayList<>();
        List<AyBehaviorTaskSon> startTaskSons = new ArrayList<>();
        //启动任务
        ayBehaviorTaskSonService.startTask(insertTaskSons, startTaskSons, ayBehaviorTaskSonArrayList, ayBatchVo);
        //判断ayBatchVo的成功数量是否大于0
        if (ayBatchVo.getSuccess() > 0) {
            //处理主任务状态
            ayBehaviorTaskService.taskStatusHandle(ayTaskIdDto.getParentId(), TaskStatusEnum.CREATED.getValue());
        }
        return ResponseResult.ok(ayBatchVo);
    }


    @SaCheckPermission("analyze:behavior:son:stop")
    @OpLog(msg = "停止行为分析子任务", opType = OpTypeEnum.OTHER)
    @ApiOperation("停止行为分析子任务")
    @PostMapping({"/stop"})
    public ResponseResult<AyBatchVo> stop(@RequestBody AyTaskIdDto taskIdDto) {
        //验证参数并获取可以停止的子任务
        AyBatchVo ayBatchVo = new AyBatchVo();
        List<AyBehaviorTaskSon> ayBehaviorTaskSonArrayList = ayBehaviorTaskSonService.canStopAyBehaviorTaskSon(taskIdDto, ayBatchVo);
        //构建停止任务的集合
        List<AyBehaviorTaskSon> stopTaskSons = new ArrayList<>();
        //停止任务
        boolean stop = ayBehaviorTaskSonService.stop(stopTaskSons, ayBehaviorTaskSonArrayList, ayBatchVo);
        //判断stop是否为true
        if (stop) {
            //遍历stopTaskSons
            for (AyBehaviorTaskSon ayBehaviorTaskSon : stopTaskSons) {
                //判断TaskId是否为空
                if (ayBehaviorTaskSon.getTaskId() != null) {
                    //TODO 发送到消息队列
                    ayBehaviorTaskSonService.sendMessage(ayBehaviorTaskSon);
                }
            }
            //处理主任务状态
            ayBehaviorTaskService.taskStatusHandle(taskIdDto.getParentId(), TaskStatusEnum.FINISHED.getValue());
        }
        return ResponseResult.ok(ayBatchVo);
    }


    @SaCheckPermission("analyze:behavior:son:plan")
    @OpLog(msg = "子任务设置布防计划", opType = OpTypeEnum.OTHER)
    @ApiOperation("子任务设置布防计划")
    @PostMapping({"/plan/add"})
    public ResponseResult<Void> sonTaskAddPlan(@Validate @RequestBody AyBehaviorTaskPlanDto ayBehaviorTaskPlanDto) {
        //调用service层方法
        boolean isSuccess = ayBehaviorTaskSonService.sonTaskAddPlan(ayBehaviorTaskPlanDto);
        if (isSuccess) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.fail("子任务添加布防计划失败");
        }
    }


    @SaCheckPermission("analyze:behavior:son:plandetail")
    @OpLog(msg = "子任务布防计划详情查看", opType = OpTypeEnum.SELECT)
    @ApiOperation("子任务布防计划详情查看")
    @GetMapping({"/plan/detail/{id}"})
    public ResponseResult<AyBehaviorTaskPlanVo> sonTaskPlanDetail(@PathVariable("id") Integer id) {
        //调用service层方法
        AyBehaviorTaskPlanVo ayBehaviorTaskSonVo = ayBehaviorTaskSonService.sonTaskPlanDetail(id);
        return ResponseResult.ok(ayBehaviorTaskSonVo);
    }


//    @SaCheckPermission("analyze:behavior:son:deleteplan")
//    @OpLog(msg = "批量删除子任务布防计划信息", opType = OpTypeEnum.OTHER)
//    @ApiOperation("批量删除子任务布防计划信息")
//    @PostMapping({"/plan/delete"})
//    public ResponseResult<Void> sonTaskDeletePlan(@RequestBody List<Integer> ids) {
//        //调用service层方法
//        boolean isSuccess = ayBehaviorTaskSonService.sonTaskDeletePlan(ids);
//        if (isSuccess) {
//            return ResponseResult.ok();
//        } else {
//            return ResponseResult.fail("子任务布防计划删除失败");
//        }
//    }
}